package com.huluobo.a2307douyindemo.setting

import androidx.lifecycle.MutableLiveData
import com.huluobo.lib_base.mvvm.BaseViewModel
import com.huluobo.lib_base.net.ApiService
import com.huluobo.lib_base.net.Const
import dagger.hilt.android.lifecycle.HiltViewModel
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import javax.inject.Inject

/**
 *  Created by LC on 2025/9/26.
 */
@HiltViewModel
class SettingViewModel @Inject constructor(private val apiService: ApiService) : BaseViewModel() {

    val uploadSuccess by lazy { MutableLiveData<String>() }

    //参数是文件路径
    fun uploadIcon(filePath: String) {
        //1.创建文件对象
        val file = File(filePath)
        //2.封装body
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), file)
        //3.封装为part  参数名,文件名,body
        val part = MultipartBody.Part.createFormData(Const.PARAM_FILE, file.name, body)
        executeNetwork(
            { apiService.uploadIcon(part) },
            uploadSuccess::postValue
        )
    }
}